home *** CD-ROM | disk | FTP | other *** search
- /* Miscellaneous format conversion subroutines */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #include "global.h"
- #include "netuser.h"
- #include "misc.h"
-
- int net_error;
-
- #define LINELEN 256
-
- /* Convert Internet address in ascii dotted-decimal format (44.0.0.1) to
- * binary IP address
- */
- int32 aton(register char *s)
- {
- int32 n;
- register int i;
-
- n = 0;
- for(i=24;i>=0;i -= 8){
- n |= (int32)atoi(s) << i;
- if((s = strchr(s,'.')) == NULLCHAR)
- break;
- s++;
- }
- return n;
- }
- /* Resolve a host name into an IP address. IP addresses in dotted-decimal
- * notation are distinguished from domain names by enclosing them in
- * brackets. e.g., [44.64.0.1]
- */
- int32 resolve(char *host)
- {
- int argc;
- char *argv[10];
- int i;
- char *s;
- char hostent[LINELEN];
- FILE *sfile;
- static struct {
- char *name;
- int32 address;
- } cache;
-
- if(*host == '['){
- /* Brackets indicate IP address in dotted-decimal form */
- return aton(host + 1);
- }
- if(cache.name != NULLCHAR && strcmp(cache.name,host) == 0)
- return cache.address;
-
- /* Not a numerical IP address, search the host table */
- if((sfile = fopen(hosts,"r")) == NULL){
- return 0;
- }
- while (fgets(hostent,LINELEN,sfile) != NULL){
- s = strtok(hostent,"\r\n\t ");
- if (s != NULL && *s == '#')
- continue;
- for (argc = 0, i = 0; i < 10 && s != NULL; i++){
- argv[argc++] = s;
- s = strtok(NULL,"\r\n\t ");
- }
- for (i = 1; i < argc; i++){
- if (argv[i][0] == '#')
- break;
- if (strcmp(host,argv[i]) == 0){
- /* Found it, put in cache */
- fclose(sfile);
- if(cache.name != NULL)
- free(cache.name);
- cache.name = strdup(host);
- cache.address = aton(argv[0]);
- return cache.address;
- }
- }
- }
- /* No address found */
- fclose(sfile);
- return 0;
- }
- /* Convert an internet address (in host byte order) to a dotted decimal ascii
- * string, e.g., 255.255.255.255\0
- */
- char *inet_ntoa(int32 a)
- {
- static char buf[16];
-
- sprintf(buf,"%u.%u.%u.%u",
- hibyte(hiword(a)),
- lobyte(hiword(a)),
- hibyte(loword(a)),
- lobyte(loword(a)) );
- return buf;
- }
- /* Convert a socket (address + port) to an ascii string of the form
- * aaa.aaa.aaa.aaa:ppppp
- */
- char *psocket(struct socket *s)
- {
- static char buf[30];
-
- sprintf(buf,"%s:%u",inet_ntoa(s->address),s->port);
- return buf;
- }
- /* Convert hex-ascii string to long integer */
- long htol(char *s)
- {
- long ret;
- char c;
-
- ret = 0;
- while((c = *s++) != '\0'){
- c &= 0x7f;
- if(c >= '0' && c <= '9')
- ret = ret*16 + (c - '0');
- else if(c >= 'a' && c <= 'f')
- ret = ret*16 + (10 + c - 'a');
- else if(c >= 'A' && c <= 'F')
- ret = ret*16 + (10 + c - 'A');
- else
- break;
- }
- return ret;
- }
-